package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.Pay_CurrentTimeBean;
import com.fhnew.app.model.network.entity.req.Pay_SingleCzBean;
import com.fhnew.app.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BankTransfer extends Activity {

    ImageView close;
    TextView titletext;
    TextView remark2;
    Button copyname;
    TextView payee;
    Button copyaccent;
    TextView payaddress;
    Button copypayaddress;
    TextView insertnum;
    TextView inserttime;
    EditText insertname;
    ImageView dizhi;
    ImageView dizhi2;
    Button next;
    private String imgurl;
    private String imgurl2;
    private String rechTime;
    private String money;
    private String cfgId;
    ClipboardManager cm;
    ClipData mClipData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_transfer);
        Constants.backgroundvoic = false;
        close = findViewById(R.id.close);
        titletext = findViewById(R.id.titletext);
        remark2 = findViewById(R.id.remark2);
        copyname = findViewById(R.id.copyname);
        payee = findViewById(R.id.payee);
        copyaccent = findViewById(R.id.copyaccent);
        payaddress = findViewById(R.id.payaddress);
        copypayaddress = findViewById(R.id.copypayaddress);
        insertnum = findViewById(R.id.insertnum);
        inserttime = findViewById(R.id.inserttime);
        insertname = findViewById(R.id.insertname);
        dizhi = findViewById(R.id.dizhi);
        dizhi2 = findViewById(R.id.dizhi2);
        next = findViewById(R.id.next);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //获取剪贴板管理器：
        cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        copypayaddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClipData = ClipData.newPlainText("Label", payaddress.getText().toString().trim());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        copyname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClipData = ClipData.newPlainText("Label", remark2.getText().toString().trim());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        copyaccent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClipData = ClipData.newPlainText("Label", payee.getText().toString().trim());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
            }
        });
        payaddress.setText(getIntent().getStringExtra("pageDesc"));
        cfgId=getIntent().getStringExtra("cfgId");
        remark2.setText(getIntent().getStringExtra("name"));
        payee.setText(getIntent().getStringExtra("paycode"));
        insertnum.setText(getIntent().getStringExtra("money"));
        money = getIntent().getStringExtra("money");
        imgurl = getIntent().getStringExtra("img");
        imgurl2 = getIntent().getStringExtra("img2");
        Glide.with(BankTransfer.this).load(imgurl).placeholder(R.mipmap.nodataimg).into(dizhi);
        Glide.with(BankTransfer.this).load(imgurl2).placeholder(R.mipmap.nodataimg).into(dizhi2);
        gettime();
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(insertname.getText().toString().trim())) {
                    save();
                } else {
                    Toast.makeText(BankTransfer.this, "请输入您的真实姓名", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //获取服务器时间
    private void gettime() {
        HttpJsonUtils.getHttpApiInstance()
                .getTime(Constants.getuseAdress() + "api/m/get/date", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_CurrentTimeBean>() {
                    @Override
                    protected void onCustomNext(Pay_CurrentTimeBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                inserttime.setText(resBanner.getData() + "");
                                rechTime = resBanner.getData() + "";
                            } else {
                                Toast.makeText(BankTransfer.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(BankTransfer.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    //提交
    private void save() {
        HttpJsonUtils.getHttpApiInstance()
                .getSingleCz(Constants.getuseAdress() + "api/m/bank/save", cfgId, money, insertname.getText().toString().trim(),
                        rechTime, UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_SingleCzBean>() {
                    @Override
                    protected void onCustomNext(Pay_SingleCzBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                Toast.makeText(BankTransfer.this, "提交成功", Toast.LENGTH_LONG).show();
                                finish();
                            } else {
                                Toast.makeText(BankTransfer.this, resBanner.getMsg(), Toast.LENGTH_LONG).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(BankTransfer.this, "异常错误，请重新操作", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

}
